package io.wit.modules.wit.controller;

import io.wit.common.utils.PageUtils;
import io.wit.common.utils.Querys;
import io.wit.common.utils.R;
import io.wit.config.ConfigFile;
import io.wit.modules.sys.controller.AbstractController;
import io.wit.modules.wit.entity.SubjectEntity;
import io.wit.modules.wit.service.SubjectService;
import io.wit.tool.ToolClient;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 经营主体
*/
@RestController
@RequestMapping("subject")
public class SubjectController extends AbstractController{

    @Autowired
	private SubjectService subjectService;

    @Value("${role_value2}")
    private long value2;
	
	/**
	 * 列表
	*/
	@RequestMapping("/list")
	@RequiresPermissions("subject:list")
	public R list(@RequestParam Map<String, Object> params){
        final String username = getUserName();
        final Long roleId = getRoleId();
        final Querys query = new Querys(params);
        if(username.equals(ConfigFile.KEY_SUPER) || roleId == value2){
            final List<SubjectEntity> subjectList = subjectService.queryList(query);
            final int total = subjectService.queryTotal(query);
            final PageUtils page = new PageUtils(subjectList,total,query.getLimit(),query.getPage());
            return R.ok().put("page",page);
        }else{
            final Integer options = getOptions();
            if(options == 2 || options == 3){}else{
                final Long subjectId = getSubjectId();
                if(subjectId != null){
                    query.put("subjectId",subjectId);
                }else{
                    final PageUtils page = new PageUtils(new ArrayList<>(),0,query.getLimit(),query.getPage());
                    return R.error("你尚未添加经营主体").put("page",page);
                }
            }
            final List<SubjectEntity> subjectList = subjectService.queryList(query);
            final int total = subjectService.queryTotal(query);
            final PageUtils page = new PageUtils(subjectList,total,query.getLimit(),query.getPage());
            return R.ok().put("page",page);
        }
	}

	/**
	 * 信息
	*/
	@RequestMapping("/info/{dataId}")
	@RequiresPermissions("subject:info")
	public R info(@PathVariable("dataId") Long dataId){
		final SubjectEntity subject = subjectService.queryObject(dataId);
		return R.ok().put("subject", subject);
	}

	/**
	 * 查询信息
	*/
	@RequestMapping("/queryMap/{dataId}")
	@RequiresPermissions("subject:info")
	public void queryMap(@PathVariable("dataId") Long dataId,final HttpServletResponse response){
        ToolClient.responseJson(subjectService.queryMap(dataId),response);
	}

    /**基地类型*/
    @GetMapping("/getTypeIdentity")
    public void getTypeIdentity(final HttpServletResponse response){
        ToolClient.responseJson(subjectService.getTypeIdentity(),response);
    }

    /**
     * 保存
    */
    @RequestMapping("/save")
    @RequiresPermissions("subject:save")
    public void save(final HttpServletRequest request,final HttpServletResponse response){
        final Long roleId = getRoleId();
        if(roleId == value2 || roleId == -1){
            final String json = ToolClient.createJsonFail("无权限操作");
            ToolClient.responseJson(json,response);
        }else{
            final String json = subjectService.save(request);
            ToolClient.responseJson(json,response);
        }
    }

    /**
     * 修改
    */
    @PostMapping("/update")
    @RequiresPermissions("subject:update")
    public void update(final HttpServletRequest request,final HttpServletResponse response){
        final Long roleId = getRoleId();
        if(roleId == value2 || roleId == -1){
            final String json = ToolClient.createJsonFail("无权限操作");
            ToolClient.responseJson(json,response);
        }else{
            final String json = subjectService.update(request);
            ToolClient.responseJson(json,response);
        }
    }
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("subject:delete")
	public R delete(@RequestBody Long[] dataIds){
		subjectService.deleteBatch(dataIds);
		return R.ok();
	}
}